package com.tucc.hello.leetcode.no1_400.no1_200.no81_100.no98;

import com.tucc.hello.tree.Main;
import com.tucc.hello.tree.TreeNode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        TreeNode root = Main.makeTree(Arrays.asList(2, 2, 2));
        System.out.println(solution.isValidBST(root));
        System.out.println(solution.values);
    }

    private List<Integer> values = new ArrayList<>();

    public boolean isValidBST(TreeNode root) {
        if (root != null) {
            boolean result = true;
            if (root.left != null) {
                result &= isValidBST(root.left);
            }
            if (!result) {
                return false;
            }
            if (values.size() > 0) {
                if (values.get(values.size() - 1) >= root.val) {
                    return false;
                }
            }
            values.add(root.val);
            if (root.right != null) {
                result &= isValidBST(root.right);
            }
            if (!result) {
                return false;
            }
        }
        return true;
    }


}
